import java.util.Random;


public class Table {
	
	String [] ID={"PL1","PL2","PL3","PL4","PL5","PL6"};
	int number=0;
	String Smallblind,Bigblind;
	String[][] sorted= new String[number][4];
	int Dealerbutton=9 ;
	int cp=0;
	String [][] check;
	int waitingp=0;
	String[] waiting;
	int line=0;
	String startingcash;
	String [] System={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
	String currentplayer;
	String rnd="0";
	Random select=new Random();
	
	public void createID(int playersnm){
		waiting=new String[playersnm];
		check=new String[playersnm][4];
		String [] players=new String[playersnm];
		for (int i=0; i<playersnm; i++){
			players[i]=ID[i];
		}
		ID=players;
	}
	
	public String getID(){
		if(number==ID.length){
			while(true){
				return("0");
			}
		}
		else{
			number++;
			return(ID[number-1]);
			}
		}
	
	public void paramsetup(String small,String big,String cash,int playersnm){
		currentplayer=ID[playersnm-1];

		startingcash=cash;
		if(Dealerbutton==9){
			Dealerbutton= select.nextInt(playersnm-1);
		}
		else{
			if(Dealerbutton==0){
				Dealerbutton=playersnm-1;
			}
			else
			Dealerbutton--;
		}
		Smallblind=small;
		Bigblind=big;
	}
	
	public String round0(String IDCODE){

		if(ID[Dealerbutton].equals(IDCODE)){
			line=0;
			rnd="0";

			return("D "+"0 "+"You_have_Dealer_Button");
			
		}
		else if( ((ID[number-1].equals(IDCODE)) && Dealerbutton==0)||((Dealerbutton!= 0)&&(ID[Dealerbutton-1].equals(IDCODE)))){
			return("S "+Smallblind+" "+ "Taking_small_blind");
		}
		else if( ((ID[0].equals(IDCODE)) && Dealerbutton==number-1)||((Dealerbutton!=number-1)&&(ID[Dealerbutton+1].equals(IDCODE)))){
			return("B "+Bigblind+" "+"Taking_big_blind");
		}
		else 
			return("N "+"0  "+"zaczynamy_gre");
		
	}
	public void hierarchy(String set){
		String[] crd=null;
		String[] help=new String[4];
		int a=0;
		int xnmb=0;
		crd=set.split(" ");
		for(int i=0; i<13;i++){
			for(int j=0; j<4; j++)
			{
				if(crd[j].charAt(0)==System[i].charAt(0)){
					help[a]=crd[j];
					a++;
				}
			}
		}
		a=0;
		for(int i=0; i<4; i++){
			for(int j=i+1; j<4; j++){
				if(((help[i].charAt(0)==help[j].charAt(0))||(help[i].endsWith("s")&help[j].endsWith("s"))||(help[i].endsWith("d")&help[j].endsWith("d"))||(help[i].endsWith("c")&help[j].endsWith("c"))||(help[i].endsWith("h")&help[j].endsWith("h")))&&(help[i]!="x")){
					help[j]="x";
				xnmb++;
				}
			}
		}
		for(int i=0; i<13;i++){
			for(int j=0; j<4; j++)
			{
				if(help[j].charAt(0)==System[i].charAt(0)){
					crd[a]=help[j];
					a++;
				}
			}
		}
		for(int i=0; i<4; i++){
				if(help[i]=="x"){
					crd[3-(xnmb-1)]=help[i];
				xnmb--;
				}
		}
		
		
			for(int j=0; j<4;j++){
				check[line][j]=crd[j];
			}
			line++;
			if(rnd.equals("0")){
			if(Dealerbutton==0){
				Dealerbutton=number-1;
				rnd="1";
			}
			else
			Dealerbutton--;
			rnd="1";
			}
	}
	
	public String[][] sort(String tab[][]){
		String [][] help2=new String[number][4];
		int [] xnmb=new int[number];
		int b,a;
		int index=0;
		for(int i=0; i<number; i++){
			xnmb[i]=0;
		}
		for(int i=0; i<number; i++){
			for(int j=0; j<4; j++){
				if(tab[i][j]=="x"){
					xnmb[i]++;
				}
			}
		}
		do{
			b=xnmb[index];
			a=index;
			for(int i=0; i<number; i++){
				if(b>xnmb[i]){
					b=xnmb[i];
					a=i;
				}
				else if(b==xnmb[i]){
					for(int q=3; q>=0; q--){
						int d=0;
						int e=0;
						for(int p=0; p<13; p++){
							if(tab[a][q].charAt(0)==System[p].charAt(0))
								d=p;
						}
						for(int p=0; p<13; p++){
							if(tab[i][q].charAt(0)==System[p].charAt(0))
								e=p;
						}
						if(e<d){
							b=xnmb[i];
							a=i;
							break ;
						}
						else if(e>d)
							break;
						
					}
				}
			}
			for(int k=0; k<4; k++){
				help2[index][k]=tab[a][k];
			}
			index++;
			xnmb[a]=5;
		}while(index<=number-1);
		return help2;
	}
	
	public String ifdraw(String[][] tab){
		int [] xnmb=new int[number];
		int d=0;
		int e=9;
		int same=0;
		for(int i=0; i<number; i++){
			xnmb[i]=0;
		}
		for(int i=0; i<number; i++){
			for(int j=0; j<4; j++){
				if(tab[i][j]=="x"){
					xnmb[i]++;
				}
			}
		}
		if(xnmb[0]==xnmb[1]){
			for(int q=3; q>=0; q--){
			
				for(int p=0; p<13; p++){
					if(tab[0][q].charAt(0)==System[p].charAt(0))
						d=p;
				}
				for(int p=0; p<13; p++){
					if(tab[1][q].charAt(0)==System[p].charAt(0))
						e=p;
				}
				if(e==d)
					same++;
			}
		}
		if(same==4)
			return ("1");
		else
			return ("0");
	}
	public String payment(String data){// pobiera id, 4karty, czy allin,allin wartosc.
		String winner="0";
		String[] param=null;
		param = data.split(" ");

		sorted=sort(check);
		for(int i=0; i<5; i++ ){
			if(sorted[0][0].equals(param[i])){
				winner="1";
				
			}
		}
		if(winner.equals("1"))
			return("Y "+"0 "+param[6]+" "+number);
		else
			return("N "+ifdraw(check));
		
		
	}
	
	public  void deleteplayer(String id){
		String[] newID=new String[number-1];
		int index=0;
		for(int i=0; i<number; i++){
			if(id!=ID[i]){
				newID[index]=ID[i];
				index++;
			}
		}
		ID=newID;
		number=number-1;
		Dealerbutton= select.nextInt(number-1);
	}
	public String wait(String id){
		int num=0;
		int index=0;
		for(int i=0; i<number; i++){
			if(!id.equals(waiting[i]))
				num++;
		}

		if(num==number){
			waiting[cp]=id;
			cp++;
			waitingp++;
		}
		for(int i=0; i<number; i++){
			if(id.equals(ID[i]))
				index=i;
		
		}
		if(index==0){
			if(waitingp!=number){
				return("0");
			}
			else{
				num=0;
				waitingp=0;
				cp=0;
				currentplayer=ID[index];
				waiting=new String[number];
				return("1");
			}
		}
			else{
				if(!ID[index-1].equals(currentplayer)){
					return("0");	
				}
				else{
					currentplayer=ID[index];
					return("1");
				}
			}
			



		
	}
}
		


